package romaremez.teachingSystem;


import java.util.List;

public class Authorizer {
    private List<User> users;
    private User user;
    private UsersHolder holder;

    public User authorizeUser(User someUser) {
        user = null;
        authenticateUser(someUser);
        return user;
    }

    private boolean authenticateUser(User someUser) {
        if (users.contains(someUser)) {
            if (!checkPassword(someUser)) {
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    private boolean checkPassword(User someUser) {
        int userIndex = users.indexOf(someUser);
        if (someUser.getPassword().equals(users.get(userIndex).getPassword())) {
            user = users.get(userIndex);
            return true;
        } else {
            return false;
        }
    }

    public void acceptNewUser(User someUser) {
        holder.saveUserProgress(someUser);
        users.add(someUser);
    }

    public void setHolder(UsersHolder holder) {
        this.holder = holder;
        setUserList();
    }

    private void setUserList() {
        this.users = holder.getUserList();
    }
}
